﻿@using Topro.Entity.Enum;
@model Topro.Entity.Response.Role.RoleResponse
@{
    Layout = "~/Views/Shared/_Dialog.cshtml";
}
@section HeadCss{
    <link href="@Url.Content("~/lib/ztree/ztreestyle.css")" rel="stylesheet" />
    <link href="@Url.Content("~/lib/icheck/skins/all.css")" rel="stylesheet" />
    <style>
        .usermenu {
            background-color: #f5f5f5;
            padding: 5px 20px;
            overflow-x: hidden;
            overflow-y: auto;
            height: 310px;
        }
    </style>
}
@section headScripts{
    <script type="text/javascript" src="@Url.Content("~/lib/ztree/jquery.ztree.all.js")"></script>
}
<section class="container-fluid" style="background-color: white;">

    <ul class="nav nav-tabs m15_t" id="myTab">
        <li class="active"><a href="#home" data-toggle="tab">基本信息</a></li>
        <li><a href="#permissions" data-toggle="tab">角色权限</a></li>
    </ul>
    <form class="form-horizontal m15_t" id="form" onsubmit="return false;">
        <div class="tab-content">
            <input type="hidden" id="Id" name="Id" value="@(Model.Id)">
            <div class="tab-pane fade in active" id="home">
                <div class="form-group">
                    <label class="wd100 control-label"><span class="WdateFmtErr">*</span>角色名称：</label>
                    <div class="wd250 m15_l">
                        <input class="form-control" type="text" name="name" value="@(Model.Name)" maxlength="40" autocomplete="off" placeholder="长度必须介于 2 和 40 之间">
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"><span class="WdateFmtErr">*</span>状态：</label>
                    <div class="wd250 m15_l">
                        <label class="m10_r">
                            <input type="radio" name="status" value="@((int)RoleStatus.启用)" @(Model.Status.Equals((int)RoleStatus.启用) ? "checked" : "")> 启用
                        </label>
                        <label>
                            <input type="radio" name="status" value="@((int)RoleStatus.禁用)" @(Model.Status.Equals((int)RoleStatus.禁用) ? "checked" : "")> 禁用
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"></label>
                    <div class="wd250 m15_l">
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"></label>
                    <div class="wd250 m15_l">
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"></label>
                    <div class="wd250 m15_l">
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"></label>
                    <div class="wd250 m15_l">
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"></label>
                    <div class="wd250 m15_l">
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"></label>
                    <div class="wd250 m15_l">
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"></label>
                    <div class="wd250 m15_l">
                    </div>
                </div>
                <div class="form-group">
                    <label class="wd100 control-label"></label>
                    <div class="wd250 m15_l">
                    </div>
                </div>
            </div>
            <!--菜单-->
            <div class="tab-pane fade" id="permissions">
                <div class="form-group">
                    <div class="usermenu">
                        <div class="zTreeDemoBackground left">
                            <ul id="ztree" class="ztree"></ul>
                        </div>
                    </div>
                </div>
            </div>

        </div>

    </form>

</section>
@section footerScript{
    <script type="text/javascript">

        var setting = {
            check: {
                enable: true,
                chkboxType: { "Y": "ps", "N": "ps" }
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0
                }
            }
        };

        var ztree,roleId=@(Model.Id);

        $(function () {

            Topro.Util.Ajax({
                type: "POST",
                url: "/Auth/GetZTree",
                data: { roleId: roleId },
                success: function (response) {
                    if (response != null) {
                        //更具返回结果初始化zTree
                        ztree = $.fn.zTree.init($("#ztree"), setting, response);
                        //默认为收起所以节点
                        ztree.ExpandAllStatus = false;
                    }
                },
                error: function (e) {
                    FlashPay.UI.Tip_short_warning("操作失败！请查看网络，请重试。");
                }
            });

        });

        function save() {

            var role = $("#form").serializeJSON();

            var selectedNodes = ztree.getCheckedNodes(true);
            //选中的菜单、权限
            var selectedAuth = new Array();

            if (selectedNodes.length > 0) {
                for (var i = 0; i < selectedNodes.length; i++) {

                    var _resourceId = selectedNodes[i].id.replace("m_", "").replace("p_", "");
                    var _resourceType = selectedNodes[i].nodetype;

                    var auth = {
                        roleId: roleId,
                        resourceId: _resourceId,
                        resourceType: _resourceType,
                    };
                    selectedAuth.push(auth);
                }
            }

            var cover = Topro.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });

            Topro.Util.Ajax({
                type: "POST",
                url: "/Auth/SaveRole",
                data: { request: role, selectedAuth: selectedAuth},
                success: function (res) {
                    if (res.success) {
                        cover.Remove();
                        Topro.UI.DialogOpener().Tip_success("操作成功！");
                        Topro.UI.DialogOpener().page.search();
                        Topro.UI.CloseDialog();
                    } else {
                        cover.Remove();
                        Topro.UI.Tip_short_warning("操作失败！" + res.errorMessage);
                    }
                },
                error: function (json) {
                    cover.Remove();
                    Topro.UI.Tip_warning("操作失败！请查看网络，请重试。");
                }
            });

        };
    </script>
}